/*
 * @Description:
 * @version:
 * @Author: 尹鹏孝
 * @Date: 2025-03-17 20:51:36
 * @LastEditors: 尹鹏孝
 * @LastEditTime: 2025-03-17 22:37:36
 */
// src/services/order-number.service.ts
import { Injectable } from '@nestjs/common';
import { randomBytes } from 'crypto';
import { format } from 'winston';
import moment from 'moment';
 const { Snowflake } = require('nodejs-snowflake');

@Injectable()
export class OrderNumberService {
  generateOrderNumber(type:string): string {

      //  const timestamp = Date.now().toString(); // 获取当前时间戳
   //   console.log('timestamp:', 111)
     const newDate = new Date(); // 获取当前时间戳
     const year = newDate.getFullYear(); // 获取当前时间戳
     const month = `${ newDate.getMonth()+1}`.padStart(2,'0'); // 获取当前时间戳
     const day = `${newDate.getDate()}`.padStart(2,'0'); // 获取当前时间戳
     const timestamp = `${year}${month}${day}`
   //  console.log('timestamp2:',timestamp)
    const randomPart = randomBytes(4).toString('hex'); // 生成随机数部分
    const config = {
    custom_epoch:Date.now(), // Defaults to Date.now(). This is UNIX timestamp in ms
    instance_id: Math.floor(4095*Math.random()) // A value ranging between 0 - 4095. If not provided then a random value will be used
    }

   //   console.log('1-21:', randomPart)
   //   console.log('1-22:',config)
     const uid = new Snowflake(config);
     const uniId = uid.getUniqueID()
     console.log('1-23:',uniId)
        // 创建一个雪花算法生成器
     //var bigId =  uid.idFromTimestamp(Date.now()); // Here id will always be BigInt
     //console.log('1-25:',bigId)
   //  return `${timestamp}-${randomPart}-${id}`; // 组合时间戳和随机数生成订单号
    const number =`${type}${timestamp}-${randomPart}${uniId}`; // 组合时间戳和随机数生成订单号
     return number
  }
}
